package com.vitabuybuy.tv.ui.mine.talent.usercenter.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.BankCardListDto
import com.vitabuybuy.tv.dto.BillListDto
import com.vitabuybuy.tv.dto.TalentWalletDto
import com.vitabuybuy.tv.inter.OnBankCardClickListener
import com.vitabuybuy.tv.inter.OnBillListClickListener
import com.vitabuybuy.tv.network.TalentWalletRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class TalentWalletViewModel : BaseViewModel() {

    val myWallet = MutableLiveData<TalentWalletDto>()

    val recordList = ObservableArrayList<BillListDto>()
    private val itemOnClickBillListener= object : OnBillListClickListener {
        override fun onItemClick(item: BillListDto) {
            defUI.msgEvent.postValue(MessageModel(2, obj = item))
        }
    }
    val recordBinding: OnItemBind<BillListDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, _: BillListDto ->
            itemBinding.set(BR.itemBean, R.layout.item_withdraw_record)
                .bindExtra(BR.listenner, itemOnClickBillListener)
        }

    val accountList = ObservableArrayList<BankCardListDto>()
    private val itemOnClickListener = object : OnBankCardClickListener {
        override fun onItemClick(item: BankCardListDto) {
            defUI.msgEvent.postValue(MessageModel(1, obj = item))
        }
    }
    val accountBinding: OnItemBind<BankCardListDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, _: BankCardListDto ->
            itemBinding.set(BR.itemBean, R.layout.item_withdraw_account)
                .bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }


    fun getMyWallet() {
        rxHttpRequest {
            onRequest = {
                myWallet.value = TalentWalletRepository.getMyWallet().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun withDraw(bankCardId: Int, withAmount: String) {
        rxHttpRequest {
            onRequest = {
                TalentWalletRepository.withDraw(withAmount, bankCardId).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }


    fun getBankCardList(pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                val it = TalentWalletRepository.getBankCardList(pageNumber, 10).await()
                if (pageNumber == 1) {
                    accountList.clear()
                }
                accountList.addAll(it)
                if (accountList.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(2, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    fun addNewBankCard(params: Map<String, Any>) {
        rxHttpRequest {
            onRequest = {
                TalentWalletRepository.addNewBankCard(params).await()
                defUI.msgEvent.postValue(MessageModel(3))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getBillList(
        type: String?,
        pageNumber: Int
    ) {
        rxHttpRequest {
            onRequest = {
                val it = TalentWalletRepository.getBillList(type, pageNumber, 10).await()
                if (pageNumber == 1) {
                    recordList.clear()
                }
                recordList.addAll(it)
                if (recordList.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }
}
